home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / dcopobject.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  13.6 KB  |  411 lines

  1. /*
  2. Copyright (c) 1999,2000 Preston Brown <pbrown@kde.org>
  3. Copyright (c) 1999, 2000 Matthias Ettrich <ettrich@kde.org>
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11.  
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14.  
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  18. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  19. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22.  
  23. #ifndef _DCOPOBJECT_H
  24. #define _DCOPOBJECT_H
  25.  
  26. #include <qobject.h>
  27. #include <qmap.h>
  28. #include <qstring.h>
  29. #include <qptrlist.h>
  30. #include <qvaluelist.h>
  31. #include <kdatastream.h> // needed for proper bool marshalling
  32. #include "kdelibs_export.h"
  33.  
  34. class DCOPClient;
  35. typedef QValueList<QCString> QCStringList;
  36.  
  37. // Makros for DCOP interfaces
  38.  
  39. #define K_DCOP \
  40. public:        \
  41.   virtual bool process(const QCString &fun, const QByteArray &data, QCString& replyType, QByteArray &replyData); \
  42.   QCStringList functions(); \
  43.   QCStringList interfaces(); \
  44. private:
  45.  
  46. #define k_dcop_signals public
  47. #define k_dcop_hidden public
  48. #define k_dcop public
  49. #define ASYNC void
  50.  
  51. /**
  52.  * Provides an interface for receiving DCOP messages.
  53.  *
  54.  * This class provides an interface for receiving DCOP messages.  To use it,
  55.  * simply multiply-inherit from DCOPObject and from some other class, and
  56.  * then implement the DCOPObject::process() method.  Because this method is
  57.  * pure virtual, you must implement the method.
  58.  *
  59.  * Note that it is usually more convenient to mark a section in the class with
  60.  * "k_dcop:", add your DCOP methods there and let the IDL compiler do the rest.
  61.  * Read the tutorials for more information.
  62.  *
  63.  * @see DCOPClient
  64.  * @see DCOPObjectProxy
  65.  * @author Preston Brown <pbrown@kde.org>, Matthias Ettrich <ettrich@kde.org>
  66.  */
  67. class DCOP_EXPORT DCOPObject
  68. {
  69. public:
  70.   /**
  71.    * Creates a DCOPObject and calculates the object id
  72.    * using its physical memory address.
  73.    */
  74.   DCOPObject();
  75.   /**
  76.    * Creates a DCOPObject and calculates the object id
  77.    * using QObject::name().
  78.    * @param obj the object to extract the name from
  79.    */
  80.   DCOPObject(QObject *obj);
  81.   /**
  82.    * Creates a DCOPObject with object Id @p objId.
  83.    * @param objId the object id of the DCOP object
  84.    */
  85.   DCOPObject(const QCString &objId);
  86.   /**
  87.    * Destroys the DCOPObject and removes it from the map
  88.    * of known objects.
  89.    */
  90.   virtual ~DCOPObject();
  91.  
  92.   /**
  93.    * Returns the object id of the DCOPObject.
  94.    * @return the object's id
  95.    */
  96.   QCString objId() const;
  97.  
  98.   /**
  99.    * Renames a dcop object, if no other with the same name exists
  100.    * Use with care, all dcop signals are disconnected
  101.    * 
  102.    * @param objId the new object id
  103.    **/
  104.   bool setObjId(const QCString &objId);
  105.  
  106.   /**
  107.    * Dispatches a message.
  108.    *
  109.    * Usually you want to use an IDL
  110.    * compiler to automatically generate an implementation for
  111.    * this function.
  112.    *
  113.    * If this function returns false, then processDynamic()
  114.    * is called.
  115.    *
  116.    * Note to implementators: remember to call the baseclasses
  117.    * implementation. It handles the functions "functions()" and
  118.    * "interfaces()" which return the lists of supported functions
  119.    * and interfaces, respectively.
  120.    *
  121.    * @param fun is the normalized function signature.
  122.    *            Such a signature usually looks like
  123.    *            foobar(QString,int). The return type,
  124.    *            qualifiers like "const" etc. are not part of
  125.    *            the signature.
  126.    * @param data the received data
  127.    * @param replyType write the reply type in this string
  128.    * @param replyData write the reply data in this array
  129.    * @return true if successful, false otherwise. The default implementation
  130.    *         returns false for all @p fun except "functions()" and
  131.    *         "interfaces()".
  132.    *
  133.    * @see DCOPClient::normalizeFunctionSignature()
  134.    * @see functions()
  135.    * @see DCOPClient::process()
  136.    */
  137.   virtual bool process(const QCString &fun, const QByteArray &data,
  138.                QCString& replyType, QByteArray &replyData);
  139.  
  140.  
  141.   /**
  142.    * This function is of interest when you used an IDL compiler
  143.    * to generate the implementation for process() but
  144.    * you still want to dispatch some functions dynamically.
  145.    * Dynamically means that methods may appear and vanish
  146.    * during runtime.
  147.    *
  148.    * @param fun is the normalized function signature.
  149.    *            Such a signature usually looks like
  150.    *            foobar(QString,int). The return type,
  151.    *            qualifiers like "const" etc. are not part of
  152.    *            the signature.
  153.    * @param data the received data
  154.    * @param replyType write the reply type in this string
  155.    * @param replyData write the reply data in this array
  156.    * @return true if successful, false otherwise. The default implementation
  157.    *         returns always false.
  158.    * @see process()
  159.    * @see DCOPClient::normalizeFunctionSignature()
  160.    * @see functions(),
  161.    * @see DCOPClient::process()
  162.    */
  163.   virtual bool processDynamic(const QCString &fun, const QByteArray &data,
  164.                   QCString& replyType, QByteArray &replyData);
  165.  
  166.    /**
  167.    * This function is of interest when you used an IDL compiler
  168.    * to generate the implementation for functions() but
  169.    * you still want to list some functions dynamically.
  170.    * Dynamically means that the methods may appear and vanish
  171.    * during runtime.
  172.    *
  173.    * @return A list of the additional functions, default is an empty list.
  174.    *
  175.    * @see functions(),
  176.    */
  177.   virtual QCStringList functionsDynamic();
  178.  
  179.     /**
  180.    * This function is of interest when you used an IDL compiler
  181.    * to generate the implementation for interfaces() but
  182.    * you still want to list some interfaces dynamically.
  183.    * Dynamically means that they may appear and vanish
  184.    * during runtime.
  185.    *
  186.    * @return A list of the additional interfaces, default is an empty list.
  187.    *
  188.    * @see interfaces(),
  189.    */
  190.   virtual QCStringList interfacesDynamic();
  191.  
  192.   /**
  193.    * Returns the names of the interfaces, specific ones last. The
  194.    * functions gets reimplemented by the IDL compiler. If you don't
  195.    * use the IDL compiler, consider implementing this function
  196.    * manually if you want your object to be easily explorable.
  197.    *
  198.    * @return a list of interfaces
  199.    * @see functions()
  200.    */
  201.   virtual QCStringList interfaces();
  202.  
  203.   /**
  204.    * Returns the list of functions understood by the object. It gets
  205.    * reimplemented by the IDL compiler. If you don't use the IDL
  206.    * compiler, consider implementing this function manually if you
  207.    * want your object to be easily scriptable.
  208.    *
  209.    * Rationale: functions() allows an interpreter to do client-side
  210.    * type-casting properly.
  211.    *
  212.    * Note to implementators: remember to call the baseclasses
  213.    * implementation.
  214.    *
  215.    * @return a list of functions
  216.    * @see interfaces()
  217.    * @see process()
  218.    * @see processDynamic()
  219.    * @see DCOPClient::normalizeFunctionSignature()
  220.    */
  221.   virtual QCStringList functions();
  222.  
  223.   /**
  224.    * Emit @p signal as DCOP signal from this object with @p data as
  225.    * arguments
  226.    * @param signal the signal to emit
  227.    * @param data the data to send
  228.    */
  229.   void emitDCOPSignal( const QCString &signal, const QByteArray &data);
  230.  
  231.   /**
  232.    * Connects to a DCOP signal.
  233.    * @param sender the name of the client that emits the signal. When empty
  234.    * the signal will be passed from any client.
  235.    * @param senderObj the name of the sending object that emits the signal.
  236.    * @param signal the name of the signal. The arguments should match with slot.
  237.    * @param slot The name of the slot to call. Its arguments should match with signal.
  238.    * @param Volatile If true, the connection will not be reestablished when
  239.    * @p sender unregisters and reregisters with DCOP. In this case the @p sender
  240.    * must be registered when the connection is made.
  241.    * If false, the connection will be reestablished when @p sender reregisters.
  242.    * In this case the connection can be made even if @p sender is not registered
  243.    * at that time.
  244.    *
  245.    * @return false if a connection could not be established.
  246.    * This will be the case when
  247.    * @li @p Volatile is true and @p sender  does not exist.
  248.    * @li @p signal and @p slot do not have matching arguments.
  249.    */
  250.   bool connectDCOPSignal( const QCString &sender, const QCString &senderObj,
  251.                           const QCString &signal,
  252.                           const QCString &slot,
  253.                           bool Volatile);
  254.  
  255.   /**
  256.    * Disconnects a DCOP signal.
  257.    *
  258.    * A special case is when both @p sender & @p signal are empty. In this
  259.    * case all connections related to this object in the current client
  260.    * are disconnected. (Both connections from as well as to this object!)
  261.    *
  262.    * @param sender the name of the client that emits the signal.
  263.    * @param senderObj the name of the object that emits the signal.
  264.    * If empty all objects will be disconnected.
  265.    * @param signal the name of the signal. The arguments should match with slot.
  266.    * If empty all objects will be disconnected.
  267.    * @param slot The name of the slot the signal is connected to.
  268.    * If empty all slots will be disconnected.
  269.    *
  270.    * @return false if no connection(s) where removed.
  271.    */
  272.   bool disconnectDCOPSignal( const QCString &sender, const QCString &senderObj,
  273.                              const QCString &signal,
  274.                              const QCString &slot);
  275.  
  276.   /**
  277.    * Returns the DCOPClient responsible for making the call. 
  278.    * Only call this function while you are handling a DCOP call.
  279.    * @return the DCOPClient responsible for making the call. 
  280.    * This information is only guaranteed to be correct when 
  281.    * entering your DCOP function.
  282.    * @since 3.1
  283.    */
  284.   DCOPClient *callingDcopClient();
  285.  
  286.   /**
  287.    * @internal Sets DCOPClient returned by callingDcopClient()
  288.    * @since 3.1
  289.    */
  290.   void setCallingDcopClient(DCOPClient *);
  291.  
  292.   /**
  293.    * Checks whether an object with the given id is known in this process.
  294.    * @return true if an object with the questionable @p objId is
  295.    *         known in this process. This query does not ask proxies.
  296.    *
  297.    * DCOPObjectProxy
  298.    */
  299.   static bool hasObject(const QCString &objId);
  300.  
  301.   /**
  302.    * Try to find a dcop object with the given id.
  303.    * This function does not query the DCOPObjectProxy.
  304.    * @param objId the object id to search
  305.    * @return the  DCOPObject for the id @p objId.
  306.    */
  307.   static DCOPObject *find(const QCString &objId);
  308.  
  309.  
  310.   /**
  311.    * Tries to find an object using a partial object id.
  312.    * This function is used for multicasting a DCOP message to
  313.    * several objects inside a single process.
  314.    * 
  315.    * @param partialId the partial object id to search for
  316.    * @return a list of DCOPObjects beginning with the string
  317.    * contained in @p partialId.
  318.    */
  319.   static QPtrList<DCOPObject> match(const QCString &partialId);
  320.  
  321.   /**
  322.    * Creates an object id for the QObject @p obj. This is done
  323.    * using the QObject::name() function.
  324.    * @param obj the object whose name will be used
  325.    * @return the created object id
  326.    */
  327.   static QCString objectName( QObject* obj );
  328.  
  329. private:
  330.   /**
  331.    * The object id of this DCOPObject.
  332.    */
  333.   QCString ident;
  334.  
  335. protected:
  336.   virtual void virtual_hook( int id, void* data );
  337. private:
  338.   class DCOPObjectPrivate;
  339.   DCOPObjectPrivate *d;
  340. };
  341.  
  342. class DCOPObjectProxyPrivate;
  343. /**
  344.  * You must use a proxy if you want to dispatch method calls for
  345.  * object IDs which don't have (yet) a corresponding DCOPObject.
  346.  * This is somewhat like virtual object references in CORBA.
  347.  *
  348.  * @see DCOPObject
  349.  * @see DCOPClient
  350.  * @author Matthias Ettrich <ettrich@kde.org>
  351.  */
  352. class DCOP_EXPORT DCOPObjectProxy
  353. {
  354. public:
  355.     /**
  356.      * Creates a new proxy.
  357.      */
  358.     DCOPObjectProxy();
  359.  
  360.     /**
  361.      * Obsolete, do not use. DCOP clients know about object proxies
  362.      * automatically.
  363.      *
  364.      * @deprecated
  365.      */
  366.     DCOPObjectProxy( DCOPClient*);
  367.  
  368.     /**
  369.      * Destroy the proxy.
  370.      */
  371.     virtual ~DCOPObjectProxy();
  372.  
  373.     /**
  374.      * Reimplement this method to dispatch method calls.
  375.      *
  376.      * This method is called of all proxies if the DCOPClient
  377.      * knows no object with the id @p obj. If the first proxy returns
  378.      * @p true, the DCOPClient will no longer call other proxies.
  379.      *
  380.      * The object id @p obj may be empty for app-wide function calls no
  381.      * associated with any object.
  382.      * @param obj the id of the object
  383.      * @param fun is the normalized function signature.
  384.      *            Such a signature usually looks like
  385.      *            foobar(QString,int). The return type,
  386.      *            qualifiers like "const" etc. are not part of
  387.      *            the signature.
  388.      * @param data the received data
  389.      * @param replyType write the reply type in this string
  390.      * @param replyData write the reply data in this array
  391.      * @return true if successful, false otherwise. The default implementation
  392.      *         returns always false.
  393.      */
  394.     virtual bool process( const QCString& obj, const QCString& fun,
  395.               const QByteArray& data,
  396.               QCString& replyType, QByteArray &replyData );
  397. private:
  398.     void* unused;
  399.     void* unused_too;
  400.     friend class DCOPClient;
  401.     static QPtrList<DCOPObjectProxy>* proxies;
  402. protected:
  403.     virtual void virtual_hook( int id, void* data );
  404. private:
  405.     DCOPObjectProxyPrivate* d;
  406. };
  407.  
  408.  
  409.  
  410. #endif
  411.